package cn.iocoder.yudao.module.dbit.controller.admin.jobinfo;

import cn.iocoder.yudao.module.dbit.dal.dataobject.jobinfo.StudentCheckJobInfoDO;
import cn.iocoder.yudao.module.dbit.service.jobinfo.JobCollectionService;
import cn.iocoder.yudao.module.dbit.service.jobinfo.StudentCheckJobInfoService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;

import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.dbit.controller.admin.jobinfo.vo.*;
import cn.iocoder.yudao.module.dbit.dal.dataobject.jobinfo.JobInfoDO;
import cn.iocoder.yudao.module.dbit.service.jobinfo.JobInfoService;

@Tag(name = "管理后台 - 企业职位信息")
@RestController
@RequestMapping("/dbit/student-job-info")
@Validated
public class StudentCheckJobInfoController {

    @Resource
    private StudentCheckJobInfoService jobInfoService;

    @Resource
    private JobCollectionService jobCollectionService;


    @GetMapping("/get")
    @Operation(summary = "获得企业职位信息")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
//    @PreAuthorize("@ss.hasPermission('dbit:job-info:query')")
    public CommonResult<StudentCheckJobInfoDO> getJobInfo(@RequestParam("id") Integer id) {
        StudentCheckJobInfoDO jobInfo = jobInfoService.getJobInfo(id);
        return success(BeanUtils.toBean(jobInfo, StudentCheckJobInfoDO.class));
    }


    @GetMapping("/collection")
    @Operation(summary = "职位收藏")
    public CommonResult<Integer> createJobCollectionInfo(@RequestParam("jobId")Integer jobId) {
        return success(jobCollectionService.createCollectionInfo(jobId));
    }

    @GetMapping("/page")
    @Operation(summary = "获得企业职位信息分页")
//    @PreAuthorize("@ss.hasPermission('dbit:job-info:query')")
    public CommonResult<PageResult<StudentCheckJobInfoDO>> getJobInfoPage(@Valid JobInfoPageReqVO pageReqVO) {
        PageResult<StudentCheckJobInfoDO> pageResult = jobInfoService.getJobInfoPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, StudentCheckJobInfoDO.class));
    }

    @GetMapping("/recommend")
    @Operation(summary = "推荐职位")
    public CommonResult<List<StudentRecommendJobInfoVO>> recommendJobInfo(@RequestParam("userId") Integer userId) {
        List<StudentRecommendJobInfoVO> result = jobInfoService.recommendJobInfo(userId);
        return success(result);
    }

    @GetMapping("/latest")
    @Operation(summary = "最新职位")
    public CommonResult<List<StudentRecommendJobInfoVO>> latestJobInfo() {
        List<StudentRecommendJobInfoVO> result = jobInfoService.latestJobInfo();
        return success(result);
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出企业职位信息 Excel")
    @PreAuthorize("@ss.hasPermission('dbit:job-info:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportJobInfoExcel(@Valid JobInfoPageReqVO pageReqVO,
                                   HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<StudentCheckJobInfoDO> list = jobInfoService.getJobInfoPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "企业职位信息.xls", "数据", JobInfoRespVO.class,
                BeanUtils.toBean(list, JobInfoRespVO.class));
    }

}